<?php echo $this->render('header.phtml'); ?>
<?php echo $this->render('menu.phtml'); ?>
<div id="content">
    <div id="icon-link" class="icon32"><br/></div><h2>编辑链接</h2>
    <div id="tabs">
        <ul>
            <li><a href="#tabs-1">列表</a></li>
            <li><a href="#tabs-2">添加</a></li>
            <li><a href="#tabs-3">编辑</a></li>
        </ul>
        <div id="tabs-1">
            <table class="list-table">
                <thead class="ui-state-default">
                    <tr>
                        <th width="5%">ID</th>
                        <th width="20%">名称</th>
                        <th width="25%">描述</th>
                        <th width="20%">地址</th>
                        <th width="15%">排序</th>
                        <th width="15%">编辑</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($this->linkList)) : ?>
                        <?php foreach ($this->linkList as $link) : ?>
                    <tr>
                        <td><?php echo $this->escape($link->linkId) ?></td>
                        <td><?php echo $this->escape($link->siteName) ?></td>
                        <td><?php echo $this->escape($link->siteDesc) ?></td>
                        <td><?php echo $this->escape($link->siteUrl) ?></td>
                        <td><?php echo $this->escape($link->displayOrder) ?></td>
                        <td>
                            <a href="javascript:void(0)" onclick="editlink(<?php echo $this->escape($link->linkId) ?>)">编辑</a> |
                            <a href="javascript:if(confirm('确实要删除【<?php echo $this->escape($link->siteName) ?>】站点链接吗?'))location='/backend/del/link/id/<?php echo $this->escape($link->linkId) ?>'">删除</a>
                        </td>
                    </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
                <tfoot class="ui-state-default">
                    <tr>
                        <th width="5%">ID</th>
                        <th width="20%">名称</th>
                        <th width="25%">描述</th>
                        <th width="20%">地址</th>
                        <th width="15%">排序</th>
                        <th width="15%">编辑</th>
                    </tr>
                </tfoot>
            </table>
            <?php if($this->linkId == 0 && !empty($this->linkList)) echo $this->paginationControl($this->linkList,'Elastic','pagebar.phtml'); ?>
        </div>
        <div id="tabs-2" class="editor-area">
            <form action="/backend/add/link/" method="post" onsubmit="return submitAdd();">
                <div class="titlebox">
                    <h3><span>名称</span></h3>
                    <input type="text" class="title" id="add_site_name" name="site_name" autocomplete="off" value="" tabindex="1" size="30" />
                </div>
                <div>
                    <h3><span>描述</span></h3>
                    <input type="text" class="title" id="add_site_desc" name="site_desc" autocomplete="off" value="" tabindex="1" size="30" />
                </div>
                <div>
                    <h3><span>地址</span></h3>
                    <input type="text" class="title" id="add_site_url" name="site_url" autocomplete="off" value="" tabindex="1" size="30" />
                </div>
                <div id="other">
                    <h3><span>类型</span></h3>
                    <?php if(!empty($this->linkType)): ?>
                    <ul>
                            <?php foreach ($this->linkType as $linkType) : ?>
                        <li><input type="radio" name="add_type_id" value="<?php echo $this->escape($linkType->typeId) ?>"><?php echo $this->escape($linkType->typeName) ?></li>
                            <?php endforeach; ?>
                    </ul>
                    <?php else: ?>
                    暂无链接类型，请先添加链接类型!
                    <?php endif; ?>
                </div>
                <div id="add-message-box" class="ui-state-error ui-corner-all" style="display: none;padding: 12px;">
                    <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 12px;"></span>
                        <strong>警告:</strong> <span id="add-error-message"></span>
                    </p>
                </div>
                <div>
                    <input id="publish" type="submit" value="添加" name="publish"/>
                </div>
            </form>
        </div>
        <div id="tabs-3" class="editor-area">
            <form action="/backend/edit/link/" method="post" onsubmit="return submitEdit();">
                <div class="titlebox">
                    <h3><span>名称</span></h3>
                    <input type="text" class="title" id="edit_site_name" name="site_name" autocomplete="off" tabindex="1" size="30" value="<?php echo $this->escape($this->linkInfo->siteName) ?>" />
                </div>
                <div>
                    <h3><span>描述</span></h3>
                    <input type="text" class="title" id="edit_site_desc" name="site_desc" autocomplete="off" tabindex="1" size="30" value="<?php echo $this->escape($this->linkInfo->siteDesc) ?>" />
                </div>
                <div>
                    <h3><span>地址</span></h3>
                    <input type="text" class="title" id="edit_site_url" name="site_url" autocomplete="off" tabindex="1" size="30" value="<?php echo $this->escape($this->linkInfo->siteUrl) ?>" />
                </div>
                <div>
                    <h3><span>排序</span></h3>
                    <input type="text" class="title" id="edit_display_order" name="display_order" autocomplete="off" tabindex="1" size="2" value="<?php echo $this->escape($this->linkInfo->displayOrder) ?>" />
                </div>
                <div id="other">
                    <h3><span>类型</span></h3>
                    <ul>
                        <?php foreach ($this->linkType as $linkType) : ?>
                        <li><input type="radio" name="edit_type_id" value="<?php echo $this->escape($linkType->typeId) ?>" <?php if($linkType->typeId == $this->linkInfo->typeId): ?>checked="true"<?php endif; ?> /><?php echo $this->escape($linkType->typeName) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </div>
                <div id="edit-message-box" class="ui-state-error ui-corner-all" style="display: none;padding: 12px;">
                    <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: 12px;"></span>
                        <strong>警告:</strong> <span id="edit-error-message"></span>
                    </p>
                </div>
                <div>
                    <input id="publish" type="submit" value="修改" name="publish"/>
                    <input id="linkId" type="hidden" value="<?php echo $this->linkInfo->linkId ?>" name="linkId" />
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function() {
        $("#tabs").tabs({
<?php if($this->linkId > 0): ?>
            disabled: [0],
<?php else: ?>
            disabled: [2],
<?php endif; ?>
            collapsible: true
        });
    });

    function submitAdd(){
        var add_type_id = 0;
        $("input[type=radio][name=add_type_id]").each(function(){
            if(this.checked) {
                add_type_id = this.value;
            }
        });

        if ($("#add_site_name").val() == '') {
            $("#add-message-box").show();
            $("#add-error-message").empty();
            $("#add-error-message").append("请输入链接名称!");
            return false;
        } else if ($("#add_site_desc").val() == '') {
            $("#add-message-box").show();
            $("#add-error-message").empty();
            $("#add-error-message").append("请输入链接介绍!");
            return false;
        } else if ($("#add_site_url").val() == '') {
            $("#add-message-box").show();
            $("#add-error-message").empty();
            $("#add-error-message").append("请输入链接地址!");
            return false;
        } else if (add_type_id == 0){
            $("#add-message-box").show();
            $("#add-error-message").empty();
            $("#add-error-message").append("请选择链接类型!");
            return false;
        } else {
            return true;
        }
    }

    function submitEdit(){
        var edit_type_id = 0;
        $("input[type=radio][name=edit_type_id]").each(function(){
            if(this.checked) {
                edit_type_id = this.value;
            }
        });

        if ($("#edit_site_name").val() == '') {
            $("#edit-message-box").show();
            $("#edit-error-message").empty();
            $("#edit-error-message").append("请输入链接名称!");
            return false;
        } else if ($("#edit_site_desc").val() == '') {
            $("#edit-message-box").show();
            $("#edit-error-message").empty();
            $("#edit-error-message").append("请输入链接介绍!");
            return false;
        } else if ($("#edit_site_url").val() == '') {
            $("#edit-message-box").show();
            $("#edit-error-message").empty();
            $("#edit-error-message").append("请输入链接地址!");
            return false;
        } else if ($("#edit_display_order").val() == '') {
            $("#edit-message-box").show();
            $("#edit-error-message").empty();
            $("#edit-error-message").append("请输入链接排序!");
            return false;
        } else if (edit_type_id == 0){
            $("#edit-message-box").show();
            $("#edit-error-message").empty();
            $("#edit-error-message").append("请选择链接类型!");
            return false;
        } else {
            return true;
        }
    }

    function editlink(id){
        $("#tabs").tabs('option', 'disable', []);
        window.location.href = '/backend/list/link/id/'+ id + '#tabs-' + 3;
    }
</script>
<?php echo $this->render('footer.phtml'); ?>